﻿/*start define*/
var styleTypeId = 0;
var normalSize = 0;
var link = {};
var myDesign = {};
/*end define*/

/*start global datas*/
var globalDatas = {};

function resetGlobalDatas() {
    globalDatas.ParentMaterials = {};
    globalDatas.ParentStones = {};
    globalDatas.ChildMaterials = {};
    globalDatas.ChildStones = {};
}

/*end global datas*/

/*start my design*/
function resetMyDesign() {
    myDesign.StyleTypeId = styleTypeId;

    myDesign.ParentStyleId = 0;

    myDesign.ParentMaterialId = 0;
    myDesign.ParentPriceOfMaterial = 0;

    myDesign.DefaultSize = normalSize;
    myDesign.ParentSize = normalSize;
    myDesign.PriceOfDifferentSize = 0;

    myDesign.ParentStoneId = 0;
    myDesign.ParentPriceOfStone = 0;

    myDesign.ChildStyleId = 0;

    myDesign.ChildMaterialId = 0;
    myDesign.ChildPriceOfMaterial = 0;

    myDesign.ChildStoneId = 0;
    myDesign.ChildPriceOfStone = 0;
}

function getPrice() {
    return myDesign.ParentPriceOfMaterial +(myDesign.PriceOfDifferentSize * (myDesign.ParentSize-myDesign.DefaultSize)) + myDesign.ParentPriceOfStone + myDesign.ChildPriceOfMaterial + myDesign.ChildPriceOfStone;

}

function buildParamForGetImage() {
    var curLink = link.GetImage;
    curLink += '?parentStyleId=' + myDesign.ParentStyleId;
    curLink += '&ParentMaterialId=' + myDesign.ParentMaterialId;
    curLink += '&ParentStoneId=' + myDesign.ParentStoneId;
    curLink += '&ChildStyleId=' + myDesign.ChildStyleId;
    curLink += '&ChildMaterialId=' + myDesign.ChildMaterialId;
    curLink += '&ChildStoneId=' + myDesign.ChildStoneId;
    curLink += '&isThumnail=' + false;
    return curLink;
}

function getImageForSelectedOption() {
    myDesign.isThumnail = false;
    $.ajax({
        url: link.GetImage,
        type: "get",
        data: myDesign,
        contentType: "application/x-www-form-urlencoded",
        success: function(result) {
            $('#main_design_image').attr('src', result);
        }
    });
}

/*end my design*/

/*start parent style*/
function parentLoadMaterialStoneChildStyle(styleId) {
    $.post(link.parentLoadMaterialStoneChildStyle, { styleId: styleId }, function(result) {
        if (result.length == 4) {
            if (result[0].length > 0) {
                var parentMaterialHtml = '';
                var parentmaterials = result[0];
                for (var i = 0; i < parentmaterials.length; i++) {
                    parentMaterialHtml += '<option value="' + parentmaterials[i].Id + '">' + parentmaterials[i].Name + '</option> ';
                    globalDatas.ParentMaterials["item-" + parentmaterials[i].Id] = parentmaterials[i];
                }
                $('#parent_Material').html(parentMaterialHtml);
                //gan gia tri material and gia tien de hien thi
                parentMaterialChanged();
            } else {

            }

            if (result[1].length > 0) {
                //show stone
                var parentStoneHtml = '';
                var parentStones = result[1];

                for (i = 0; i < parentStones.length; i++) {
                    parentStoneHtml += '<img src="' + link.GetImageBySize + '?imageName=' + parentStones[i].ThumnailImage + '&imageType=1" onclick="parentStoneChanged(' + parentStones[i].Id + ')"/>';
                    globalDatas.ParentStones["item-" + parentStones[i].Id] = parentStones[i];
                }
                $('#parent-Stone-total-item').html('of ' + parentStones.length);
                $('#slide_image1').html(parentStoneHtml);
                $('#parentStones').show();
                parentStoneChanged(parentStones[0].Id);

            } else
                $('#parentStones').hide();

            //show child style
            if (result[2].length > 0) {

                var childstyleHtml = '';
                var childStyles = result[2];

                for (i = 0; i < childStyles.length; i++)
                    childstyleHtml += '<img src="' + link.GetImageBySize + '?imageName=' + childStyles[i].Image + '&imageType=1" title="' + childStyles[i].Name + '" onclick="childSelectedStyle(' + i + ',' + childStyles[i].Id + ')"/>';
                $('#child-style-total-item').html('of ' + childStyles.length);
                $('#child-style-Selected-Index').html(0);

                $('#slide_image2').html(childstyleHtml);
                $('#childStyles').show();
            } else //hide child style
                $('#childStyles').hide();

            $('#styleDescription').html(unescape(result[3]));

            changeImageForSelected();
        }
    });
}

/*end parent style*/

/*start parent material*/
function parentMaterialChanged() {
    var item = globalDatas.ParentMaterials["item-" + $('#parent_Material').val()];
    if (item) {
        myDesign.ParentMaterialId = item.Id;
        myDesign.ParentPriceOfMaterial = item.Price;
        myDesign.PriceOfDifferentSize = item.PriceOfDifferentSize;
        $('#totalPrice').html(formatNumber(getPrice()));
        $('#main_design_image').attr('src', buildParamForGetImage());
    }
}

/*end parent material*/

/*start parent stone*/
function parentStoneChanged(id) {
    var item = globalDatas.ParentStones["item-" + id];
    if (item) {
        $('#parent-stone-description').html(item.Description);
        myDesign.ParentStoneId = item.Id;
        myDesign.ParentPriceOfStone = item.Price;
        $('#totalPrice').html(formatNumber(getPrice()));
        $('#main_design_image').attr('src', buildParamForGetImage());
    }
}

/*end parent stone*/

/*start child style*/

function childLoadMaterialStone(styleId) {
    $.post(link.childLoadMaterialStone, { styleId: styleId }, function(result) {
        if (result.length == 2) {
            if (result[0].length > 0) {
                //show child material
                var childMaterialHtml = '';
                var childMaterials = result[0];

                for (var i = 0; i < childMaterials.length; i++) {
                    childMaterialHtml += '<option value="' + childMaterials[i].Id + '">' + childMaterials[i].Name + '</option> ';
                    globalDatas.ChildMaterials["item-" + childMaterials[i].Id] = childMaterials[i];
                }
                $('#child_Material').html(childMaterialHtml);
                childMaterialChanged();
                $('#childMaterials').show();

            } else //hide child material
                $('#childMaterials').hide();

            if (result[1].length > 0) {
                //show child stone
                var childStoneHtml = '';
                var childStones = result[1];

                for (i = 0; i < childStones.length; i++) {
                    childStoneHtml += '<img src="' + link.GetImageBySize + '?imageName=' + childStones[i].ThumnailImage + '&imageType=1" onclick="childStoneChanged(' + childStones[i].Id + ')"/>';
                    globalDatas.ChildStones["item-" + childStones[i].Id] = childStones[i];
                }

                $('#child-stone-total-item').html('of ' + childStones.length);
                $('#slide_image3').html(childStoneHtml);
                $('#childStones').show();
            } else //hide child stone
                $('#childStones').hide();

        }
    });
}

/*end child style*/

/*start child material*/
function childMaterialChanged() {
    var item = globalDatas.ChildMaterials["item-" + $('#child_Material').val()];
    if (item) {
        myDesign.ChildMaterialId = item.Id;
        myDesign.ChildPriceOfMaterial = item.Price;
    }
    $('#totalPrice').html(formatNumber(getPrice()));
    $('#main_design_image').attr('src', buildParamForGetImage());

}

/*end child material*/

/*start child stone*/
function childStoneChanged(id) {
    var item = globalDatas.ChildStones["item-" + id];
    if (item) {
        $('#child-stone-description').html(item.Description);
        myDesign.ChildStoneId = item.Id;
        myDesign.childPriceOfStone = item.Price;
        $('#totalPrice').html(formatNumber(getPrice()));
        $('#main_design_image').attr('src', buildParamForGetImage());
    }
}

/*end child stone*/

/*start size*/
function sizeChanged() {
    var curSize = $('#style-size').val();
    if (curSize) {
        var value = parseInt(curSize);
        myDesign.ParentSize = value;
        if (styleTypeId == 3 || styleTypeId == 5) {
            $('#totalPrice').html(formatNumber(getPrice()));
        }
    }
}

/*end size*/

